<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="visible" width="800px" append-to-body :before-close="handleClose">
    <trouble-shooting-detail v-if="form.messageType == 1" :form="form" :dataStatus="dataStatus"></trouble-shooting-detail>
    <trouble-shoot-detail v-else :form="form" :dataStatus="dataStatus"></trouble-shoot-detail>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" size="mini">
      <div class="form-title">接收信息</div>
      <el-row>
        <el-col :span="12">
          <el-form-item label="接收人" prop="receiveUserName">
            {{form.receiveUserName}}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="验收天数" prop="acceptanceDays">
            <el-input type="number" v-model="form.acceptanceDays" placeholder="请输入验收天数" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="合格数量" prop="qualifiedNum">
            <el-input type="number" v-model="form.qualifiedNum" placeholder="请输入合格数量" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="不合格数量" prop="unqualifiedNum">
            <el-input type="number" v-model="form.unqualifiedNum" placeholder="请输入不合格数量" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="接收照片" prop="acceptancePicIds">
            <image-upload v-model="form.acceptancePicIds" :isShowTip="true" :isDisabled="false" :limit="100" :fileSize="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="接收意见" prop="receiveMessage">
            <el-input type="textarea" v-model="form.receiveMessage" placeholder="请输入接收意见" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  getTroubleShooting,
  updateTroubleShooting,
} from "@/api/cases/troubleShooting";
import DeptUserSelect from '@/components/Select/DeptUserSelect.vue';
import { mapGetters } from 'vuex';
import TroubleShootingDetail from '@/components/Detail/troubleShootingDetail.vue';
import TroubleShootDetail from '@/components/Detail/troubleShootDetail.vue';

export default {
  components: { DeptUserSelect, TroubleShootingDetail, TroubleShootDetail },
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      visible: false,
      // 表单参数
      form: {
        id: void 0,
        roadName: void 0,
        troubleshootingDirection: void 0,
        locationPileNumberBegin: void 0,
        locationPileNumberEnd: void 0,
        iTroubleshooting: void 0,
        iPart: void 0,
        locationNum: void 0,
        damageLevel: void 0,
        damageNum: void 0,
        numUnit: void 0,
        description: void 0,
        repairMethod: void 0,
        troubleshootingTime: void 0,
        picIds: void 0,
        auditUserId: void 0,
        deptId: void 0,
        dataStatus: void 0,
        auditMessage: void 0,
        assignorUserId: void 0,
        quantitativeTypeId: void 0,
        ownPersonnel: [],
        outSourceNum: void 0,
        receiveUserId: void 0,
        qualifiedNum: void 0,
        unqualifiedNum: void 0,
        acceptancePicIds: [],
        receiveMessage: void 0
      },
      dataStatus: void 0
    }
  },
  props: {
  },
  computed: {
    ...mapGetters(["userId"]),
    // 表单校验
    rules() {
      return {
        acceptanceDays: [
          { required: true, message: "验收天数不能为空", trigger: "blur" },
        ],
        qualifiedNum: [
          { required: true, message: "合格数量不能为空", trigger: "blur" },
        ],
        unqualifiedNum: [
          { required: true, message: "不合格数量不能为空", trigger: "blur" },
        ],
        receiveMessage: [
          { required: true, message: "接收意见不能为空", trigger: "blur" },
        ],
      }
    },
  },
  methods: {
    /**
     * 打开
     */
    open(id) {
      getTroubleShooting(id).then((response) => {
        this.form = response.data;
        this.dataStatus = this.form.dataStatus;
        this.form.qualifiedNum = this.form.taskNum;
        this.form.unqualifiedNum = 0;
        this.form.acceptanceDays = 1;
        this.form.dataStatus = '6';
        this.visible = true;
        this.title = "接收";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (Number(this.form.qualifiedNum) + Number(this.form.unqualifiedNum) != Number(this.form.taskNum)) {
            this.$modal.msgError("请确认合格数量与不合格数量是否填写正确");
            return;
          }
          let params = Object.assign({}, this.form)
          updateTroubleShooting(params).then(({ code, data, msg }) => {
            if (code == 200) {
              this.$modal.msgSuccess("接收完成");
              this.handleClose();
              this.$root.$emit("updateTable");
            } else {
              this.$modal.msgError(msg);
            }
          });
        }
      });
    },
    /**
     * 处理关闭
     */
    handleClose() {
      this.cancel();
    },
    /**
     * 隐藏
     */
    cancel() {
      this.visible = false;
    },
  }
}
</script>

<style>
</style>